<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//------------------------
// 助手函数
//-------------------------
use think\Config;

if (!function_exists('encrypt_pass')) {
   /**
  * 统一密码加密方式，如需变动直接修改此处
  * @param $password
  * @return string
  */
 function  encrypt_pass($password)
 {
     return hash("md5", trim($password).Config::get('salt'));
 }
}

if (!function_exists('get_client_ip')) {
  /**
 * 获得真实IP地址
 * @return string
 * 自学php博客www.zixuephp.cn整理
 */
  function get_client_ip() {
      static $realip = NULL;
      if ($realip !== NULL) return $realip;
      if (isset($_SERVER)) {
          if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
              $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
              foreach ($arr AS $ip) {
                  $ip = trim($ip);
                  if ($ip != 'unknown') {
                      $realip = $ip;
                      break;
                  }
              }
          } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
              $realip = $_SERVER['HTTP_CLIENT_IP'];
          } else {
              if (isset($_SERVER['REMOTE_ADDR'])) {
                  $realip = $_SERVER['REMOTE_ADDR'];
              } else {
                  $realip = '0.0.0.0';
              }
          }
      } else {
          if (getenv('HTTP_X_FORWARDED_FOR')) {
              $realip = getenv('HTTP_X_FORWARDED_FOR');
          } elseif (getenv('HTTP_CLIENT_IP')) {
              $realip = getenv('HTTP_CLIENT_IP');
          } else {
              $realip = getenv('REMOTE_ADDR');
          }
      }
      preg_match('/[\d\.]{7,15}/', $realip, $onlineip);
      $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
      return $realip;
  }
}

if (!function_exists('downloadBigFile')) {
    // 下载大文件，实现断点传输
    /**
    * $url是服务器的文件地址
    * $file是用户指定的下载后的文件名
    */
    function downloadBigFile($url, $file="", $timeout=3600) {
      $file = empty($file) ? pathinfo($url,PATHINFO_BASENAME) : $file;
      $dir = pathinfo($file,PATHINFO_DIRNAME);
      !is_dir($dir) && @mkdir($dir,0755,true);
      $url = str_replace(" ","%20",$url);

      if(function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $temp = curl_exec($ch);
        if(@file_put_contents($file, $temp) && !curl_error($ch)) {
          return $file;
        } else {
          return false;
        }
      } else {
        $opts = array(
          "http"=>array(
          "method"=>"GET",
          "header"=>"",
          "timeout"=>$timeout)
        );
        $context = stream_context_create($opts);
        if(@copy($url, $file, $context)) {
          //$http_response_header
          return $file;
        } else {
          return false;
        }
      }
    }
}


if (!function_exists('p')) {
  /**
   * 友好显示内容，多用于bug测试
   * @param string $data 要输出的类容
   */
  function p($data){
      // 定义样式
      $str='<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
      // 如果是boolean或者null直接显示文字；否则print
      if (is_bool($data)) {
          $show_data=$data ? 'true' : 'false';
      }elseif (is_null($data)) {
          $show_data='null';
      }else{
          $show_data=print_r($data,true);
      }
      $str.=$show_data;
      $str.='</pre>';
      echo $str;
  }
}

if (!function_exists('do_explode')) {
  function do_explode($src,$condition = ','){
      $data = explode($condition,$src);
      foreach ($data as $k=> $v) {
         if($v == null || $v == '')
            unset($data[$k]);
      }
      return $data;
  }
}
